package 预科0104ChattingRoom_Q5;



import java.io.*;
import java.io.File;
import java.net.Socket;
import java.util.Scanner;

public class Client0 {
    public static void main(String[] args) {
        //创建Socket类型对象并提供IP和端口号
        String strname;
        String name = "张三";
        Socket s = null;
        PrintStream printStream = null;
        Scanner scanner = null;
        File clientfile = new File(ServerTest.lockfilename + name);
        if(!clientfile.exists()){
            clientfile.mkdir();
            System.out.println(name + "文件夹创建成功！");
        }
        else{
            System.out.println(name + "文件夹已存在！");
        }


        try {
            scanner = new Scanner(System.in);
            s = new Socket("127.0.0.1",8888);
            System.out.println("连接服务器成功！");
            printStream = new PrintStream(s.getOutputStream());
            printStream.println(name);

            Clientlistener clientlistener = new Clientlistener(s,name);
            Thread td = new Thread(clientlistener);
            td.start();

            while(true){

                //向服务端发送信息
                //System.out.println("请输入需要发送的内容/文件路径：");

                String str = scanner.next();
                if("bye".equalsIgnoreCase(str)){
                    System.out.println(name + "退出聊天！");
                    break;
                }
                else if("file".equalsIgnoreCase(str)){
                    System.out.println("请输入要发送的文件：");
                    String filename = scanner.next();
                    strname = ServerTest.lockfilename + name + "/" + filename;

                    //System.out.println(str);
                    File file = new File(strname);
                    if(file.isFile()){
                        if(file.exists()) {
                            //send 传输到服务器
                            printStream.println(str);
                            //文件名
                            printStream.println(filename);
                            //传文件
                            Function.FileSend(s, file);
                        }
                        else {
                            System.out.println("文件不存在！");
                        }
                    }
                    /*else {
                        System.out.println("不支持传输文件夹！");
                    }*/

                }
                else {
                    printStream.println(str);
                    //System.out.println("发送内容成功！");
                }

            }
        }

        catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(null!=printStream){
                printStream.close();
            }
            if(null!=scanner){
                scanner.close();
            }
            //关闭Socket
            if(s!=null){
                try {
                    s.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
